import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:interview/components/app/app_ripple_button.dart';
import 'package:interview/components/tag.dart';
import 'package:interview/controller/navigation_controller.dart';
import 'package:interview/theme/app_colors.dart';

class QuestionItem extends StatefulWidget {
  final List<String> ids;
  final String id;
  final int difficulty;
  final String title;
  final int likes;
  final int views;
  final bool isViewed;
  final int typeId;

  const QuestionItem({
    super.key,
    required this.id,
    required this.difficulty,
    required this.title,
    required this.likes,
    required this.views,
    required this.ids ,
    required this.typeId,
    this.isViewed = false,
  });

  @override
  State<QuestionItem> createState() => _QuestionItemState();
}

class _QuestionItemState extends State<QuestionItem> {
  final navigation = Get.find<NavigationController>();
  void onTap (){
    navigation.pushWithAuth(
      '/questionpage' , 
      parameters: {
        "id" : widget.id , 
        "ids":widget.ids , 
        "typeId":widget.typeId,
        "views":widget.views,
      }
    );
  }

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context).extension<AppColors>()!;
    return AppRippleButton(
      rippleRadius:BorderRadius.circular(8),
      borderRadius:BorderRadius.circular(8),
      backgroundColor: theme.subjectPure!,
      splashColor:theme.contrastMix3!.withOpacity(0.2),
      highlightColor:theme.subjectMix1!.withOpacity(0.1),
      onTap: onTap,
      child: Container(
        padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start, // 内容左对齐
          children: [
            Row(
              crossAxisAlignment: CrossAxisAlignment.center, // 垂直方向居中
              children: [
                Tag(difficulty: widget.difficulty), // 动态难度标签
                const SizedBox(width: 12), // 标签和标题间距
                Expanded(
                  child: Text(
                    widget.title,
                    style: const TextStyle(
                      fontSize: 14,
                      fontWeight: FontWeight.bold,
                    ),
                    maxLines: 1,  // 最多展示两行
                    overflow: TextOverflow.ellipsis,
                  ),
                ),
              ],
            ),
            const SizedBox(height: 8), // 间距
            Row(
              crossAxisAlignment: CrossAxisAlignment.center, // 确保内容垂直居中对齐
              children: [
                _buildText("点赞 ${widget.likes}"),
                _buildText("浏览 ${widget.views}"),
                _buildText(widget.isViewed ? "已阅读" : "未阅读"),
              ],
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildText(String text) {
    return Padding(
      padding: const EdgeInsets.only(right: 8), // 添加每个文本间距
      child: Text(
        text,
        style: const TextStyle(fontSize: 10, color: Colors.grey),
      ),
    );
  }
}
